package demo.practice.leetcode;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Offer32 {


    Queue<TreeNode> s = new LinkedList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {

        s.add(root);
        List<List<Integer>> lp = new ArrayList<>();
        if(root==null)
            return lp;
        while (!s.isEmpty()) {
            int size = s.size();
            List<Integer> lc = new ArrayList<>();
            List<TreeNode> nc = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode n = s.remove();
                nc.add(n);
                lc.add(n.val);
            }
            nc.forEach(e -> {
                if (e.left != null) s.add(e.left);
                if (e.right != null) s.add(e.right);
            });
            lp.add(lc);
        }
        return lp;
    }


}
